local TinyGameItem = {}

function TinyGameItem:new(...)
    return ClassUtils:extends(TinyGameItem, Display, ...);
end

function TinyGameItem:init(i, j, checkerboard)
    self.xIndex = i;
    self.yIndex = j;
    self.isSelected = false;
    self.iconBtn = nil;
    self.icon = nil;
    self.iconName = nil;
    self.isEmpty = true;
    self:loadPrefab("TinyGame/Widget/TinyGameItem", checkerboard);
end

function TinyGameItem:onStart()
    self.iconBtn = self:find("mask/icon", CButton);
    self.icon = self:find("mask/icon", Image);

    self:setPos(TinyGameData.distance * self.xIndex, TinyGameData.distance * self.yIndex);
    self.gameObject.name=self.xIndex..""..self.yIndex;
    Utils.addClick(self.iconBtn, function()
        if #TinyGameModule.tinyGame.itemSelectedDic >= 2 then
            return;
        end
        self:setSelected(true);
        Dispatcher:dispatch(EventType.TinyGameItem_Selected, self);
    end);
end

function TinyGameItem:onShow()
    self:setEmpty(self.isEmpty);
    self:setIcon(self.iconName);
    self:setSelected(self.isSelected);
end

function TinyGameItem:onHide()

end

function TinyGameItem:setEmpty(b)
    self.isEmpty = b;
    if self.icon then
        Utils.setVisible(self.icon, not self.isEmpty);
    end
end

function TinyGameItem:setIcon(iconName)
    self.iconName = iconName;
    if self.icon and iconName then
        self.icon.sprite = ResMgr:getAtlasprite("TinyGame/TinyGameAtlas", iconName);
        Utils.setVisible(self.icon, true);
    end
end

function TinyGameItem:setSelected(b)
    self.isSelected = b;
    if self.iconBtn then
        self.iconBtn.button.interactable = not self.isSelected;
        self.iconBtn:SetGray(self.isSelected);
    end
end

return TinyGameItem;